package ipWhite

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/ipwhite"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type IpWhiteInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 详情
func NewIpWhiteInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IpWhiteInfoLogic {
	return &IpWhiteInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IpWhiteInfoLogic) IpWhiteInfo(req *types.IpWhiteInfoReq) (resp *types.IpWhiteListItemRes, err error) {

	u, err := l.svcCtx.SettingService.IpWhite.IpWhiteInfo(l.ctx, &ipwhite.IpWhiteInfoReq{Id: req.Id, MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "IpWhiteInfo err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	if u.Data.Id == 0 {
		return nil, code.NotData
	}

	resp = &types.IpWhiteListItemRes{
		Id:         u.Data.Id,
		MerchantId: u.Data.MerchantId,
		Ip:         u.Data.Ip,
		Status:     int64(u.Data.Status),
		Remark:     u.Data.Remark,
	}
	return resp, nil
}
